package com.ecoolex.ying.space.admin.graphql;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.UserWorks;
import com.ecoolex.ying.space.entity.UserWorksMarkting;
import com.ecoolex.ying.space.entity.UserWorksMarktingDetail;
import com.ecoolex.ying.space.pojo.AddUserWorksMarktingDTO;
import com.ecoolex.ying.space.pojo.AddUserWorksMarktingDetailDTO;
import com.ecoolex.ying.space.pojo.EditUserWorksMarktingDTO;
import com.ecoolex.ying.space.ws.UserWorksMarktingService;
import com.ecoolex.ying.space.ws.UserWorksService;

@GraphQL2Module(type = Type.Admin, description = "用户作品营销位管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = UserWorksMarktingGraphql.AUTH, name = "用户作品营销位管理")
public class UserWorksMarktingGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-USER-WORKS-MARKTING";
	
	@Reference
	private UserWorksMarktingService userWorksMarktingService;
	
	@Reference
	private UserWorksService userWorksService;
	
	@TypeField(type = UserWorks.class,field = "isAssociated",description = "是否关联了营销位")
	public Boolean isAssociated(@Source UserWorks source,@NonNull Integer marktingId) {
		return userWorksMarktingService.getByMarktingIdAndWorksId(marktingId, source.getWorksId())==null ? false : true;
	}
	
	@TypeField(type = UserWorksMarkting.class,field = "detailList",description = "营销位明细列表")
	public Pagination<UserWorksMarktingDetail> detailList(@Source UserWorksMarkting source,@NonNull Page page){
		return userWorksMarktingService.queryDetailByMarktingId(source.getMarktingId(), page);
	}
	
	@TypeField(type = UserWorksMarktingDetail.class,field = "userWorks",description = "作品")
	public UserWorks userWorks(@Source UserWorksMarktingDetail source) {
		return userWorksService.getUserWorksById(source.getWorksId());
	}
	
	private static final String AUTH_GET = AUTH + "-GET";

    @QueryField(field = "userWorksMarkting", description = "作品营销位")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "作品营销位")
	public UserWorksMarkting get(@NonNull Integer marktingId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_GET);
		return userWorksMarktingService.get(marktingId);
	}
    
    private static final String AUTH_QUERY = AUTH + "-QUERY";
    
    @QueryField(field = "userWorksMarktingList", description = "作品营销位列表")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "作品营销位列表")
    public Pagination<UserWorksMarkting> query(@NonNull Page page,AdminMemberAuthority user){
    	user.assertAuth(AUTH_QUERY);
    	return userWorksMarktingService.query(page);
    }
    
    private static final String AUTH_ADD = AUTH + "-ADD";
    
    @MutationField(field = "addUserWorksMarkting", description = "添加用户作品营销位")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD, name = "添加用户作品营销位")
    public UserWorksMarkting add(@NonNull AddUserWorksMarktingDTO record,AdminMemberAuthority user) {
    	user.assertAuth(AUTH_ADD);
    	return userWorksMarktingService.add(record, user.userName());
    }
    
    private static final String AUTH_EDIT = AUTH + "-EDIT";
    
    @MutationField(field = "editUserWorksMarkting", description = "编辑用户作品营销位")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT, name = "编辑用户作品营销位")
    public UserWorksMarkting edit(@NonNull EditUserWorksMarktingDTO record,AdminMemberAuthority user) {
    	user.assertAuth(AUTH_EDIT);
    	UserWorksMarkting old = userWorksMarktingService.get(record.getMarktingId());
    	Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "营销位不存在");
    	old.setPositionName(record.getPositionName());
    	old.setWeight(record.getWeight());
    	return userWorksMarktingService.edit(old, user.userName());
    }
    
    private static final String AUTH_DEL = AUTH + "-DEL";
    
    @MutationField(field = "delUserWorksMarkting", description = "删除用户作品营销位")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL, name = "删除用户作品营销位")
    public Boolean delete(@NonNull Integer marktingId,AdminMemberAuthority user) {
    	user.assertAuth(AUTH_DEL);
    	UserWorksMarkting old = userWorksMarktingService.get(marktingId);
    	Assert.isTrue(Check.notNull(old),ResultCode.VERIFY.build(1), "营销位不存在");
    	userWorksMarktingService.deleteMarkting(old, user.userName());
    	return true;
    }
    
    private static final String AUTH_ADD_DETAIL = AUTH + "-ADD-DETAIL";
    
    @MutationField(field = "addUserWorksMarktingDetail", description = "添加用户作品营销位明细")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_ADD_DETAIL, name = "添加用户作品营销位明细")
    public Boolean addDetail(@NonNull Integer marktingId,@NonNull List<AddUserWorksMarktingDetailDTO> detailList,AdminMemberAuthority user) {
    	user.assertAuth(AUTH_ADD_DETAIL);
    	userWorksMarktingService.addDetail(marktingId, detailList, user.userName());
    	return true;
    }
    
    private static final String AUTH_EDIT_DETAIL = AUTH + "-EDIT-DETAIL";
    
    @MutationField(field = "editUserWorksMarktingDetail", description = "编辑用户作品营销位明细")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT_DETAIL, name = "编辑用户作品营销位明细")
    public Boolean editDetail(@NonNull Integer id,@NonNull Integer weight,AdminMemberAuthority user) {
    	user.assertAuth(AUTH_EDIT_DETAIL);
    	UserWorksMarktingDetail detail = userWorksMarktingService.getDetail(id);
    	Assert.isTrue(Check.notNull(detail),ResultCode.VERIFY.build(1), "营销位明细不存在");
    	detail.setWeight(weight);
    	userWorksMarktingService.editDetail(detail, user.userName());
    	return true;
    }
    
    private static final String AUTH_DEL_DETAIL = AUTH + "-DEL-DETAIL";
    
    @MutationField(field = "delUserWorksMarktingDetail", description = "删除用户作品营销位明细")
    @AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_DEL_DETAIL, name = "删除用户作品营销位明细")
    public Boolean delDetail(@NonNull Integer id,AdminMemberAuthority user) {
    	user.assertAuth(AUTH_DEL_DETAIL);
    	UserWorksMarktingDetail detail = userWorksMarktingService.getDetail(id);
    	Assert.isTrue(Check.notNull(detail),ResultCode.VERIFY.build(1), "营销位明细不存在");
    	userWorksMarktingService.delDetail(detail, user.userName());
    	return true;
    }
}
